package com.edu.controller.user;

import com.edu.entity.User;
import com.edu.service.user.UserService;
import com.edu.service.user.UserServiceImpl;
import com.edu.utils.ParamFormat;
import com.edu.utils.ResponseBody;
import com.edu.utils.StringUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Author ZCN
 * @Date 2021/11/11 22:18
 * @Version 1.0
 **/
@WebServlet("/user/login")
public class LoginServlet extends HttpServlet {

    private UserService userService = new UserServiceImpl();

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException{
        try {
            ParamFormat.format(req,resp);
            String username = req.getParameter("username");
            String password = req.getParameter("password");
            // 参数校验
            StringUtil.isNull(username);
            StringUtil.isNull(password);

            // 表单信息封装user
            User user = new User(username, password);

            // 返回信息
            String rs = userService.login(user);

            // 信息返回
            ResponseBody.success(resp,rs,0L,null);
        } catch (Exception e) {
            e.printStackTrace();
            ResponseBody.failure(resp,e.getMessage());
        }
    }
}
